package com.ljtg.ljtgservice.controller.user;

import com.ljtg.ljtgpojo.dto.UserLoginDTO;
import com.ljtg.ljtgpojo.entity.User;
import com.ljtg.ljtgpojo.vo.UserLoginVO;
import com.ljtg.ljtgservice.service.UserService;
import com.ljtg.properties.JwtProperties;
import com.ljtg.result.Result;
import com.ljtg.utils.JwtUtil;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.Map;

@RestController
@RequestMapping("/user/user")
@Slf4j
@Tag(name = "用户管理", description = "用户相关的接口")
public class UserController {
    @Autowired
    private UserService userService;
    @Autowired
    private JwtProperties jwtProperties;
    @PostMapping("/login")
    @Operation(summary = "用户登录", description = "用户通过微信登录，返回用户信息和Token")
    public Result<UserLoginVO> login(
            @Parameter(name = "userLoginDTO", description = "用户登录信息", required = true)
            @RequestBody UserLoginDTO userLoginDTO) {
        User user = userService.wxlogin(userLoginDTO);
        Map<String, Object> claims = new HashMap<>();
        claims.put("userid", user.getId());
        String token = JwtUtil.createJWT(jwtProperties.getUserSecretKey(), jwtProperties.getUserTtl(), claims);
        UserLoginVO userLoginVO = UserLoginVO.builder()
                .id(user.getId())
                .openid(user.getOpenid())
                .token(token)
                .build();
        return Result.success(userLoginVO);
    }
}